package com.cmb.lk50.sink.mongo;

import com.cmb.lk50.pojo.AccessInfo;
import org.bson.BsonReader;
import org.bson.BsonWriter;
import org.bson.codecs.Codec;
import org.bson.codecs.DecoderContext;
import org.bson.codecs.EncoderContext;
import org.springframework.stereotype.Component;

/**
 * Author       : 蒋俊钊 80256973
 * Date         : 2020/8/21
 * Copyright    (C) ChinaMerchantsBank
 */
@Component
public class AccessInfoCodec implements Codec<AccessInfo> {

    private static final String SEQUENCE_ID = "sequenceId";
    private static final String HOST_NAME = "hostName";
    private static final String HOST_IP = "hostIp";
    private static final String HOST_PORT = "hostPort";
    private static final String METHOD = "method";
    private static final String PATH = "path";
    private static final String INVOKE_IP = "invokeIp";
    private static final String INVOKE_PORT = "invokePort";
    private static final String QUERY_PARAM = "queryParam";
    private static final String STATUS = "status";
    private static final String RESPONSE_CONTENT = "responseContent";
    private static final String COST_TIME = "costTime";
    private static final String LOG_TIME = "logTime";

    @Override
    public AccessInfo decode(BsonReader reader, DecoderContext decoderContext) {
        AccessInfo log = new AccessInfo();
        reader.readStartDocument();
        log.setHostName(reader.readString(HOST_NAME));
        log.setHostIp(reader.readString(HOST_IP));
        log.setHostPort(reader.readInt32(HOST_PORT));

        log.setMethod(reader.readString(METHOD));
        log.setPath(reader.readString(PATH));

        log.setInvokeIp(reader.readString(INVOKE_IP));
        log.setInvokePort(reader.readInt32(INVOKE_PORT));

        log.setQueryParam(reader.readString(QUERY_PARAM));
        log.setStatus(reader.readString(STATUS));

        log.setResponseContent(reader.readString(RESPONSE_CONTENT));
        log.setCostTime(reader.readInt64(COST_TIME));
        log.setLogTime(reader.readString(LOG_TIME));

        reader.readEndDocument();
        return log;
    }

    @Override
    public void encode(BsonWriter writer, AccessInfo log, EncoderContext encoderContext) {
        writer.writeStartDocument();
        writer.writeString(HOST_NAME, log.getHostName());
        writer.writeString(HOST_IP, log.getHostIp());
        writer.writeInt32(HOST_PORT, log.getHostPort());

        writer.writeString(METHOD, log.getMethod());
        writer.writeString(PATH, log.getPath());

        writer.writeString(INVOKE_IP, log.getInvokeIp());
        writer.writeInt32(INVOKE_PORT, log.getInvokePort());

        writer.writeString(QUERY_PARAM, getStringOrNull(log.getQueryParam()));
        writer.writeString(STATUS, getStringOrNull(log.getStatus()));
        writer.writeString(RESPONSE_CONTENT, getStringOrNull(log.getResponseContent()));

        writer.writeInt64(COST_TIME, log.getCostTime());
        writer.writeString(LOG_TIME, log.getLogTime());
        writer.writeEndDocument();
    }

    private String getStringOrNull(String str) {
        return str == null ? "" : str;
    }

    @Override
    public Class<AccessInfo> getEncoderClass() {
        return AccessInfo.class;
    }
}
